<?php
/**
 * Introduction: 配送区域逻辑处理
 * @author: 杨陈鹏
 * @date: 2016/4/11 14:25
 * @email: yangchenpeng@cdlinglu.com
 */

namespace Common\Logic;

use Common\Model;

class SellAreaLogic
{

    static public $model;

    public function __construct()
    {

        self::$model = new Model\SellAreaModel();

    }

    /**
     * Introduction: 获取区域表了的区域ID
     * @author: 杨陈鹏
     * @date: 2016/4/11 14:25
     * @email: yangchenpeng@cdlinglu.com
     * @param yangchenpeng@cdlinglu.com
     */
    static public function getSellAreaData($store_id)
    {

        if (!$store_id)
            return ['code' => 300, 'msg' => '参数错误'];

        $sell_area = self::$model
            ->where(['store_id' => $store_id])
            ->field('areas')
            ->find();

        return unserialize($sell_area['areas']) ? unserialize($sell_area['areas']) : [];

    }

    /**
     * Introduction: 获取区域地址
     * @author: 杨陈鹏
     * @date: 2016/4/11 14:25
     * @email: yangchenpeng@cdlinglu.com
     * @param yangchenpeng@cdlinglu.com
     */
    static public function getData($where, $store_id = 1, $fields = '*', $order = 'sort asc')
    {

        $area_logic = D('Common/Area', 'Logic');

        //获取数据
        $areas = $area_logic::getData($where, $fields, $order);

        $sell_area = self::$model
            ->where(['store_id' => $store_id])
            ->field('areas')
            ->find();

        $sell_area = unserialize($sell_area['areas']);
        //过滤数据
        foreach ($areas as $v) {

            if (in_array($v['id'], $sell_area))
                $data[] = $v;

        }

        return ['code' => 200, 'data' => $data];


    }

    /**
     * Introduction: 插入数据
     * @author: 杨陈鹏
     * @date: 2016/4/11 14:25
     * @email: yangchenpeng@cdlinglu.com
     * @param $data  需要插入的数据 store_id areas为必须字段
     *
     */
//    static public function create($data)
//    {
//
//        if(!$data['store_id'] || !$data['areas'])
//            return ['code' => 300 ,'msg'=>'参数错误'];
//
//        if(!is_array($data['areas']))
//            $data['areas'] = explode(',',$data['areas']);
//
//        $data['areas'] = serialize($data['areas']);
//
//        if(self::$model->add($data))
//            return ['code' => 200 ,'msg'=>'保存成功'];
//
//        return ['code' => 300 ,'msg'=>'保存失败'];
//
//    }
    /**
     * Introduction: 修改数据
     * @author: 杨陈鹏
     * @date: 2016/4/11 14:25
     * @email: yangchenpeng@cdlinglu.com
     * @param $data  修改修改的数据 store_id areas为必须字段
     *
     */
    static public function save($data)
    {

        if (!$data['store_id'] || !$data['areas'])
            return ['code' => 300, 'msg' => '参数错误'];

        $data['areas'] = serialize($data['areas']);

        if (self::$model->where(['store' => $data['store_id']])->save(['areas' => $data['areas']])) {

            return ['code' => 200, 'msg' => '保存成功'];

        } else if (self::$model->add($data)) {

            return ['code' => 200, 'msg' => '保存成功'];

        }

        return ['code' => 300, 'msg' => '保存失败'];

    }


}